﻿using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace MyDeeWeibo
{

    public class DeeDataContext : DataContext
    {
        // Pass the connection string to the base class.
        public DeeDataContext(string connectionString)
            : base(connectionString)
        { }

        // Specify a table for the accounts.
        public Table<SavedAccount> Accounts;

        // Specify a table for the categories.
        //public Table<ToDoCategory> Categories;
    }

    [Table]
    public class SavedAccount : INotifyPropertyChanged, INotifyPropertyChanging
    {

        // Define ID: private field, public property, and database column.
        private int _accountId;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int AccountId
        {
            get { return _accountId; }
            set
            {
                if (_accountId != value)
                {
                    NotifyPropertyChanging("AccountId");
                    _accountId = value;
                    NotifyPropertyChanged("AccountId");
                }
            }
        }

        // Define user name: private field, public property, and database column.
        private string _userName;

        [Column]
        public string UserName
        {
            get { return _userName; }
            set
            {
                if (_userName != value)
                {
                    NotifyPropertyChanging("UserName");
                    _userName = value;
                    NotifyPropertyChanged("UserName");
                }
            }
        }

        // Define user id: private field, public property, and database column.
        private string _userId;

        [Column]
        public string UserId
        {
            get { return _userId; }
            set
            {
                if (_userId != value)
                {
                    NotifyPropertyChanging("UserId");
                    _userId = value;
                    NotifyPropertyChanged("UserId");
                }
            }
        }

        // Define user id: private field, public property, and database column.
        private string _profileImg;

        [Column]
        public string ProfileImg
        {
            get { return _profileImg; }
            set
            {
                if (_profileImg != value)
                {
                    NotifyPropertyChanging("ProfileImg");
                    _profileImg = value;
                    NotifyPropertyChanged("ProfileImg");
                }
            }
        }

        // Define last login time: private field, public property, and database column.
        private string _lastLogin;

        [Column]
        public string LastLogin
        {
            get { return _lastLogin; }
            set
            {
                if (_lastLogin != value)
                {
                    NotifyPropertyChanging("LastLogin");
                    _lastLogin = value;
                    NotifyPropertyChanged("LastLogin");
                }
            }
        }

        // Define accessToken: private field, public property, and database column.
        private string _accessToken;

        [Column]
        public string AccessToken
        {
            get { return _accessToken; }
            set
            {
                if (_accessToken != value)
                {
                    NotifyPropertyChanging("AccessToken");
                    _accessToken = value;
                    NotifyPropertyChanged("AccessToken");
                }
            }
        }

        // Define accessToken: private field, public property, and database column.
        private string _refleshToken;

        [Column]
        public string RefleshToken
        {
            get { return _refleshToken; }
            set
            {
                if (_refleshToken != value)
                {
                    NotifyPropertyChanging("AccessToken");
                    _refleshToken = value;
                    NotifyPropertyChanged("AccessToken");
                }
            }
        }

        // Define expired value: private field, public property, and database column.
        private bool _isExpired;

        [Column]
        public bool IsExpired
        {
            get { return _isExpired; }
            set
            {
                if (_isExpired != value)
                {
                    NotifyPropertyChanging("IsExpired");
                    _isExpired = value;
                    NotifyPropertyChanged("IsExpired");
                }
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

#if sample
        // Internal column for the associated ToDoCategory ID value
        [Column]
        internal int _categoryId;

        // Entity reference, to identify the ToDoCategory "storage" table
        private EntityRef<ToDoCategory> _category;

        // Association, to describe the relationship between this key and that "storage" table
        [Association(Storage = "_category", ThisKey = "_categoryId", OtherKey = "Id", IsForeignKey = true)]
        public ToDoCategory Category
        {
            get { return _category.Entity; }
            set
            {
                NotifyPropertyChanging("Category");
                _category.Entity = value;

                if (value != null)
                {
                    _categoryId = value.Id;
                }

                NotifyPropertyChanging("Category");
            }
        }
#endif

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

#if sample
    [Table]
    public class ToDoCategory : INotifyPropertyChanged, INotifyPropertyChanging
    {

        // Define ID: private field, public property, and database column.
        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                NotifyPropertyChanging("Id");
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        // Define category name: private field, public property, and database column.
        private string _name;

        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                NotifyPropertyChanging("Name");
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        // Define the entity set for the collection side of the relationship.
        private EntitySet<SavedAccount> _todos;

        [Association(Storage = "_todos", OtherKey = "_categoryId", ThisKey = "Id")]
        public EntitySet<SavedAccount> ToDos
        {
            get { return this._todos; }
            set { this._todos.Assign(value); }
        }


        // Assign handlers for the add and remove operations, respectively.
        public ToDoCategory()
        {
            _todos = new EntitySet<SavedAccount>(
                new Action<SavedAccount>(this.attach_ToDo),
                new Action<SavedAccount>(this.detach_ToDo)
                );
        }

        // Called during an add operation
        private void attach_ToDo(SavedAccount toDo)
        {
            NotifyPropertyChanging("ToDoItem");
            toDo.Category = this;
        }

        // Called during a remove operation
        private void detach_ToDo(SavedAccount toDo)
        {
            NotifyPropertyChanging("ToDoItem");
            toDo.Category = null;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }
#endif

}